/**
 * @author lteng
 * @time 2022-06-16 22:40:24 
 */

package liteng.study.cn.stuscore.controller;

import java.io.IOException;
import java.net.URLDecoder;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import liteng.study.cn.stuscore.comm.Page;
import liteng.study.cn.stuscore.entity.Grade;
import liteng.study.cn.stuscore.service.GradeService;
import liteng.study.cn.stuscore.service.impl.GradeServiceImpl;

/**
 * 年级信息控制
 */
@WebServlet("/grade")
public class GradeServlet extends HttpServlet {
	private GradeService gradeService = new GradeServiceImpl();

	@Override
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		System.out.println("grade-get方法已启动");
		request.setCharacterEncoding("UTF-8");
		String action = request.getParameter("action");

		if (action.equals("info")) {
			descInfo(request, response);

		} else if (action.equals("page")) {
			pageInfo(request, response);

		} else if (action.equals("add")) {
			addInfo(request, response);

		} else if (action.equals("upd")) {
			updInfo(request, response);

		} else if (action.equals("del")) {
			delInfo(request, response);
		}
	}

	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		System.out.println("grade-post方法已启动");

		doGet(request, response);

	}

	/**
	 * 
	 * @param request
	 * @param response
	 * @throws IOException
	 */
	private void delInfo(HttpServletRequest request, HttpServletResponse response) throws IOException {
		// TODO Auto-generated method stub
		int id = Integer.valueOf(request.getParameter("id"));

		Grade grade = gradeService.getOne(id);

		gradeService.delete(grade);

		response.sendRedirect("/stuscore/grade?action=page&pageIndex=1&pageSize=10");

	}

	/**
	 * 
	 * @param request
	 * @param response
	 * @throws IOException
	 * @throws ServletException
	 */
	private void updInfo(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		String name = request.getParameter("name");
		
		int id = Integer.valueOf(request.getParameter("id"));

		Grade grade = gradeService.getOne(id);

		if (!name.equals(grade.getName()) && isExist(name)) {
			request.setAttribute("error", "要修改的信息已存在！");

			request.getRequestDispatcher("grade?action=info&id=" + id).forward(request, response);
		} else {
			String comm = request.getParameter("comm");
			grade.setName(name);
			grade.setComm(comm);

			gradeService.update(grade);

			response.sendRedirect("/stuscore/grade?action=page&pageIndex=1&pageSize=10");
		}
	}

	/**
	 * 
	 * @param request
	 * @param response
	 * @throws IOException
	 * @throws ServletException
	 */
	private void pageInfo(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		int pageIndex = Integer.valueOf(request.getParameter("pageIndex"));
		int pageSize = Integer.valueOf(request.getParameter("pageSize"));
//		String name = request.getParameter("name");

		String name = request.getParameter("name");
		if (name != null) {
			name = URLDecoder.decode(name, "utf-8");

		}

		Page page = null;
		if (name != null && !name.equals("")) {
			page = gradeService.getPageGrades(pageIndex, pageSize, name);

		} else {
			page = gradeService.getPageGrades(pageIndex, pageSize);

		}
		request.setAttribute("page", page);

		request.getRequestDispatcher("/pages/grade/list.jsp").forward(request, response);

	}

	/**
	 * 修改年级信息
	 * 
	 * @param request
	 * @param response
	 * @throws IOException
	 * @throws ServletException
	 */
	private void descInfo(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub

		int id = Integer.valueOf(request.getParameter("id"));

		Grade grade = gradeService.getOne(id);
		request.setAttribute("grade", grade);
		request.getRequestDispatcher("/pages/grade/upd.jsp").forward(request, response);

	}

	/**
	 * 
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void addInfo(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String name = request.getParameter("name");
		String comm = request.getParameter("comm");

		if (isExist(name)) {
			request.setAttribute("error", "要添加的信息已存在");
			request.getRequestDispatcher("/pages/grade/add.jsp").forward(request, response);

		} else {
			Grade grade = new Grade();
			grade.setName(name);
			grade.setComm(comm);

			gradeService.add(grade);

			response.sendRedirect("/stuscore/grade?action=page&pageIndex=1&pageSize=10");
		}
	}

	/**
	 * 
	 * @param name
	 * @return
	 */
	private boolean isExist(String name) {
		// TODO Auto-generated method stub
		return gradeService.isExistGrade(name);
	}

}
